# 注册登录接口
from p2p.api import log
from p2p.config import HOST


class ApiRegisterLogin:
    # 初始化
    def __init__(self,session):
        # 获取session对象
        self.session=session
        # 获取图片验证码url
        self.__url_picture_code=HOST+"/common/public/verifycode1/{}"
        # 获取短信验证码url
        self.__url_message_code=HOST+"/member/public/sendSms"

        self.__url_register=HOST+"/member/public/reg"
        self.__url_login=HOST+"/member/public/login"
        self.__url_login_status=HOST+"/member/public/islogin"

    # 封装 获取图片验证码接口
    def api_picture_code(self,random):
        log.info("正在调用获取图片验证码接口，请求url:{}".format(self.__url_picture_code.format(random)))
    #     调用get方法，返回响应数据
        return self.session.get(url=self.__url_picture_code.format(random))

    # 封装 获取短信验证码接口
    def api_message_code(self,phone,code):
        # 定义请求参数
        data={
            "phone": phone,
            "imgVerifyCode": code,
            "type": "reg"
        }
        log.info("正在调用获取短信验证码接口，请求url:{},请求参数:{}".format(self.__url_message_code, data))
        # 调用请求方法
        return self.session.post(url=self.__url_message_code,data=data)

    # 封装 注册接口
    def api_register(self,phone,pwd,code,phone_code):
        data={
            "phone": phone,
            "password": pwd,
                "verifycode": code,
        "phone_code": phone_code,
        "invite_phone":"",
        "dy_server":" on"
        }
        log.info("正在调用注册接口，请求url:{},请求参数:{}".format(self.__url_register, data))

        return self.session.post(url=self.__url_register,data=data)



    # 封装 登录接口
    def api_login(self,key="13200001130",pwd="123456"):
        data={
            "keywords":key,
            "password":pwd
        }
        log.info("正在调用登录接口，请求url:{},请求参数:{}".format(self.__url_login, data))

        return self.session.get(url=self.__url_login,data=data)


    # 封装 查询登录状态接口
    def api_login_status(self):
        log.info("正在调用查询登录状态接口，请求url:{},请求参数:{}".format(self.__url_login_status))

        return self.session.post(url=self.__url_login_status)
